﻿using System.Drawing;
using System.Windows.Forms;
using HuaCheng.Model;

namespace HuaCheng.UserControls
{
    public partial class UserBase : UserControl
    {
        public Channel channel;//传递数据
        public delegate void UcChannel_MouseClick();//定义鼠标右键委托
        public delegate void SetMenuStripEnable();//设置并联菜单委托
        public event UcChannel_MouseClick ClearBorder;//定义事件       
        public event SetMenuStripEnable RightMouseClick;
        private bool selectedStat;//选中状态
        private bool parallelStat;//并联状态
        public string path;//配置文件
        public UnitOrder uOrder;
        public UserBase()
        {
            InitializeComponent();
            //CheckForIllegalCrossThreadCalls = false;//允许线程之间的操作
        }
        #region 需要子类重写的方法
        /// <summary>
        /// 设置语言环境
        /// </summary>
        public virtual void SetResourceCulture(string language)
        {

        }
        /// <summary>
        /// 从配置文件读取单位
        /// </summary>
        public virtual void InitParam()
        {

        }
        /// <summary>
        /// 通道类 传递通道参数到前台
        /// </summary>
        public virtual Channel Channel
        {
            get { return channel; }
            set { channel = value; }
        }
        #endregion
        #region 监听事件
        public void OnMouseClick()//方法
        {
            if (ClearBorder != null)
            {
                ClearBorder();//执行委托方法
            }
        }
        public void OnRightMouseClick()
        {
            if (RightMouseClick != null)
            {
                RightMouseClick();
            }
        }
        #endregion
        #region 设置属性
        /// <summary>
        /// 通道选中状态 true为选中
        /// </summary>
        public bool SelectedStatus
        {
            get { return this.selectedStat; }
            set { this.selectedStat = value; }
        }
        /// <summary>
        /// 并联状态 true为并联
        /// </summary>
        public bool ParallelStatus
        {
            get { return this.parallelStat; }
            set { this.parallelStat = value; }
        }
        #endregion
        #region 用户控件点击事件
        /// <summary>
        /// 点击子控件时触发父控件点击事件，避免父控件点击事件被覆盖
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void lbl_MouseClick(object sender, MouseEventArgs e)
        {
            base.OnMouseClick(e);
        }
        /// <summary>
        /// 点击子控件时触发父控件点击事件，避免父控件点击事件被覆盖
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void lbl_MouseDown(object sender, MouseEventArgs e)
        {
            base.OnMouseDown(e);
        }
        /// <summary>
        /// 绘制panel边框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void chlBat_Paint(object sender, PaintEventArgs e)
        {
            UserBase chl = sender as UserBase;
            Color UcBorderColor = Color.DarkGray;
            if (chl.SelectedStatus)//若选中状态则绘制边框为黄色，否则为灰色
            {
                UcBorderColor = Color.Yellow;
            }
            ControlPaint.DrawBorder(e.Graphics,
                                        chl.ClientRectangle,
                                        UcBorderColor,
                                        3,
                                        ButtonBorderStyle.Solid,
                                        UcBorderColor,
                                        3,
                                        ButtonBorderStyle.Solid,
                                        UcBorderColor,
                                        3,
                                        ButtonBorderStyle.Solid,
                                        UcBorderColor,
                                        3,
                                        ButtonBorderStyle.Solid);
        }

        /// <summary>
        /// 鼠标左击处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void chlBat_MouseClick(object sender, MouseEventArgs e)
        {
            if (sender is UserBase)
            {
                if (e.Button == MouseButtons.Left && Control.ModifierKeys == Keys.Control)//如果按住ctrl+左键
                {
                    this.SelectedStatus = !this.SelectedStatus;
                    this.Refresh();
                }
                else
                {
                    //清空其它控件边框
                    OnMouseClick();
                    this.SelectedStatus = true;
                    this.Refresh();
                }
            }
        }
        /// <summary>
        /// 鼠标右击处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void chlBat_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (sender is UserBase)
                {
                    if (!this.SelectedStatus)
                    {
                        //清除其它选中控件边框
                        OnMouseClick();
                        this.SelectedStatus = true;
                        this.Refresh();
                    }
                    OnRightMouseClick();//设置右键并联菜单显示文本和状态
                }
            }
        }
        /// <summary>
        /// 清除borderstyle
        /// </summary>
        public void ClearSelfBorder()
        {
            this.Refresh();
        }
        #endregion
    }
}
